/*
JuffEd - A simple text editor
Copyright 2007 Murzin Mikhail

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "TextDocWidget.h"
#include "Core_Text_Edit.h"
#include "Document.h"
#include "LineNumWidget.h"
#include "TextDocSettings.h"

#include <QtGui/QResizeEvent>
#include <QtGui/QScrollBar>
#include <QtGui/QTextBlock>
#include <QtGui/QTextCursor>
#include <QtGui/QTextEdit>

TextDocWidget::TextDocWidget(Document* doc, QWidget* parent) : QWidget(parent), doc_(doc) {

	edit_ = new Core_Text_Edit(this);
	lineNumWidget_ = new LineNumWidget(edit_, this);
	lineNumWidget_->show();
	
	setFocusProxy(edit_);
	connect(edit_, SIGNAL(textChanged()), SLOT(updateLayout()));
	QScrollBar* scr = edit_->verticalScrollBar();
	connect(scr, SIGNAL(valueChanged(int)), SLOT(scrolled(int)));
	connect(edit_, SIGNAL(cursorPositionChanged()), this, SIGNAL(cursorPositionChanged()));
}

void TextDocWidget::updateLayout() {
	int h = height();
	int w = width();
	lineNumWidget_->updateWidget();
	int w1 = lineNumWidget_->width();

	lineNumWidget_->setGeometry(0, 0, w1, h);
	edit_->setGeometry(w1, 0, w - w1, h);
}

void TextDocWidget::scrolled(int) {
	updateLayout();
}

void TextDocWidget::cut() {
	edit_->cut();
}

void TextDocWidget::copy() {
	edit_->copy();
}

void TextDocWidget::paste() {
	edit_->paste();
}

void TextDocWidget::find(const QString& text, bool matchCase, bool backward) {
	QTextDocument::FindFlags flags = 0;
	if (matchCase)
		flags |= QTextDocument::FindCaseSensitively;
	if (backward)
		flags |= QTextDocument::FindBackward;
	QTextCursor cursor = edit_->textCursor();
	if (cursor.selectedText().compare(text) == 0 && flags & QTextDocument::FindBackward)
		cursor.setPosition(cursor.selectionStart());

	QTextCursor cur = edit_->document()->find(text, cursor.position(), flags);
	if (cur.position() >= 0)
		edit_->setTextCursor(cur);
}

void TextDocWidget::resizeEvent(QResizeEvent*) {
	updateLayout();
}

void TextDocWidget::setDocument(QTextDocument* doc) {
	edit_->setDocument(doc); 
}

void TextDocWidget::applySettings() {
	lineNumWidget_->applySettings();
	QFont font = TextDocSettings::font();
	edit_->setFont(font);
	int tabIndent = TextDocSettings::tabStopWidth() * QFontMetrics(font).width(" ");
	edit_->setTabStopWidth(tabIndent);
	edit_->setPlainText(edit_->toPlainText());
	if (TextDocSettings::widthAdjust())
		edit_->setLineWrapMode(QTextEdit::WidgetWidth);
	else
		edit_->setLineWrapMode(QTextEdit::NoWrap);
	resizeEvent(0);
}

void TextDocWidget::gotoLine(int line) {
	QTextBlock block = edit_->document()->begin();
	QTextCursor cur = edit_->textCursor();
	cur.setPosition(block.position());
	while (block.isValid() && cur.blockNumber() + 1 < line) {
		cur.setPosition(block.position());
		block = block.next();
	}
	edit_->setTextCursor(cur);
}

void TextDocWidget::getCursorPos(int& row, int& col) {
	QTextCursor c = edit_->textCursor();
	row = c.blockNumber();
	col = c.position() - c.block().position();
}
